#include <cstdint>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
private:
    bool cmp(TreeNode* node1, TreeNode* node2) {
        if (node1 == nullptr && node2 == nullptr) {
            return true;
        } else if (node1 == nullptr || node2 == nullptr) {
            if (node1 == nullptr) {
                return false;
            } else {
                return true;
            }
        } else if (node1->val != node2->val) {
            return false;
        } else {
            return cmp(node1->left, node2->left) && cmp(node1->right, node2->right);
        }
    }
    bool dfs(TreeNode* node1, TreeNode* node2) {
        if (node1 == nullptr) {
            return false;
        }
        if (cmp(node1, node2)) {
            return true;
        }
        return dfs(node1->left, node2) || dfs(node1->right, node2);
    }
public:
    bool isSubStructure(TreeNode* A, TreeNode* B) {
        if (B == nullptr) {
            return false;
        }
        return dfs(A, B);
    }
};